#include <getopt.h>
#include <netlink/netlink.h>

static void obj_input(struct nl_object *obj, void *arg)
{
    struct nl_dump_params dp = {
        .dp_fd = stdout,
        .dp_type = NL_DUMP_DETAILS,
    };

    nl_object_dump(obj, &dp);
}

static int my_cb(struct nl_msg *msg, void *arg)
{
#if 1
    if (nl_msg_parse(msg, &obj_input, NULL) < 0)
        fprintf(stderr, "<<EVENT>> Unknown message type\n");

    return NL_STOP;
#endif
    //return genl_handle_msg(msg, NULL);
}

int main(int argc, char *argv[])
{
    struct rtgenmsg rt_hdr = {
        .rtgen_family = AF_UNSPEC,
    };

    char msg[] = {0xde, 0xed, 0x1e, 0xe1};

    for (;;) {
        int c, optidx = 0;
        struct nl_sock *sk = NULL;
        static struct option long_opts[] = {
            { "version", 0, 0, 'v' },
            { "receive", 0, 0, 'r' },
            { "send", 0, 0, 's' },
            { 0, 0, 0, 0 }
        };

        c = getopt_long(argc, argv, "vrs", long_opts, &optidx);
        if (c == -1)
            break;

        switch (c) {
            case 'v': 
                printf("Version: 0x%x\n", LIBNL_VER_NUM);
                break;

            case 's':
                sk = nl_socket_alloc();
                nl_connect(sk, NETLINK_USERSOCK);
                nl_send_simple(sk, 0x12, 0, msg, sizeof(msg));
                nl_close(sk);
                nl_socket_free(sk);
                break;

            case 'r':
                sk = nl_socket_alloc();
                nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, my_cb, NULL);
                nl_connect(sk, NETLINK_GENERIC);
                nl_recvmsgs_default(sk);
                nl_close(sk);
                nl_socket_free(sk);
        }
    }

    return 0;
}
